package com.cl.it.custom.service.impl;

import com.cl.it.custom.model.NewsVo;
import com.cl.it.custom.service.IBizNewsService;
import com.cl.it.mbg.entity.Company;
import com.cl.it.mbg.entity.News;
import com.cl.it.mbg.service.ICompanyService;
import com.cl.it.mbg.service.INewsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IBizNewsServiceImpl implements IBizNewsService {
    @Autowired
    private ICompanyService companyService;

    @Autowired
    private INewsService newsService;

    @Override
    public IPage<NewsVo> page(String companyName, Integer curPage, Integer pageSize) {
        List<Company> companies = companyService.getBaseMapper().selectList(new QueryWrapper<>());
        Map<String, Company> companyMap = new HashMap<>();
        for (Company company : companies) {
            companyMap.put(company.getName(), company);
        }

        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(companyName)) {
            queryWrapper.eq("name", companyName);
        }
        int total = newsService.getBaseMapper().selectCount(queryWrapper);

        int start = (((curPage == null || curPage <= 0) ? 1 : curPage) - 1) * pageSize;
        queryWrapper.orderByDesc("DATE_FORMAT(LTRIM(news_date), '%Y-%m-%d')");
        queryWrapper.last(String.format(" LIMIT %s,%s", start, pageSize));
        List<News> list = newsService.getBaseMapper().selectList(queryWrapper);
        IPage<NewsVo> ipageVo = new Page<>();
        List<NewsVo> newsVos = new ArrayList<>();
        for (News news : list) {
            NewsVo vo = new NewsVo();
            BeanUtils.copyProperties(news, vo);
            vo.setShortName(companyMap.get(news.getName()).getShortName());
            vo.setOutsiteName(companyMap.get(news.getName()).getOutsiteName());
            vo.setContent(convertContent(news.getContent()));
            vo.setTitle(convertTitle(news.getTitle()));
            newsVos.add(vo);
        }
        ipageVo.setTotal(total);
        ipageVo.setRecords(newsVos);
        ipageVo.setCurrent(curPage);
        ipageVo.setSize(pageSize);
        return ipageVo;
    }

    @Override
    public NewsVo news(Long id) {
        List<Company> companies = companyService.getBaseMapper().selectList(new QueryWrapper<>());
        Map<String, Company> companyMap = new HashMap<>();
        for (Company company : companies) {
            companyMap.put(company.getName(), company);
        }
        News news = newsService.getBaseMapper().selectById(id);
        NewsVo vo = new NewsVo();
        BeanUtils.copyProperties(news, vo);
        vo.setShortName(companyMap.get(news.getName()).getShortName());
        vo.setOutsiteName(companyMap.get(news.getName()).getOutsiteName());
        vo.setContent(convertContent(news.getContent()));
        vo.setTitle(convertTitle(news.getTitle()));
        return vo;
    }

    private String convertContent(String content) {
        if (StringUtils.isEmpty(content)) {
            return content;
        }
        return content.replaceAll("\\r\\n|\\n", "<br/>");
    }

    private String convertTitle(String title) {
        if (StringUtils.isEmpty(title)) {
            return title;
        }
        if (title.indexOf("|") != -1) {
            title = title.substring(title.indexOf("|") + 1);
        } else if (title.indexOf("!") != -1) {
            title = title.substring(title.indexOf("!") + 1);
        } else if (title.indexOf("！") != -1) {
            title = title.substring(title.indexOf("！") + 1);
        } else if (title.indexOf("#") != -1) {
            title = title;
        } else if (title.indexOf("丨") != -1) {
            title = title.substring(title.indexOf("丨") + 1);
        }
        return title;
    }
}
